/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.ui;

import com.ivanbiz.dao.PenghuniDAO;
import com.ivanbiz.dao.impl.PenghuniDAOImpl;
import com.ivanbiz.model.Penghuni;
import com.ivanbiz.service.ServiceHelper;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

/**
 *
 * @author Shbt Peterpan
 */
public class PenghuniPanel extends javax.swing.JPanel {

    List<Penghuni> listPenghuni;
    Penghuni penghuniKamar;
    String kamar;
    JDialog jDialogKamar;

    public Penghuni getPenghuniKamar() {
        return penghuniKamar;
    }

    public void setPenghuniKamar(Penghuni penghuniKamar) {
        this.penghuniKamar = penghuniKamar;
    }

    public PenghuniPanel() {
        initComponents();
        refresh();
    }

    public PenghuniPanel(JDialog jDialog, String kamar) {
        initComponents();
        this.kamar = kamar;
        this.jDialogKamar = jDialog;
        refresh();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablePenghuni = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        buttonTambah = new javax.swing.JButton();
        buttonUbah = new javax.swing.JButton();
        buttonHapus = new javax.swing.JButton();

        jLabel1.setText("jLabel1");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel2.setText("Daftar Penghuni");

        tablePenghuni.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tablePenghuni.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablePenghuniMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tablePenghuni);

        buttonTambah.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/tambah.jpg"))); // NOI18N
        buttonTambah.setText("Tambah Penghuni Baru");
        buttonTambah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonTambahActionPerformed(evt);
            }
        });
        jPanel1.add(buttonTambah);

        buttonUbah.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/ubah.jpg"))); // NOI18N
        buttonUbah.setText("Ubah Penghuni Terseleksi");
        buttonUbah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonUbahActionPerformed(evt);
            }
        });
        jPanel1.add(buttonUbah);

        buttonHapus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/hapus.jpg"))); // NOI18N
        buttonHapus.setText("Hapus Penghuni Terseleksi");
        buttonHapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonHapusActionPerformed(evt);
            }
        });
        jPanel1.add(buttonHapus);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 543, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(0, 349, Short.MAX_VALUE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void buttonTambahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonTambahActionPerformed
        JDialog jDialog = new JDialog();
        jDialog.setModal(true);
        jDialog.setSize(435, 435);
        jDialog.add(new PenghuniUpdatePanel(jDialog));
        jDialog.setLocationRelativeTo(null);
        jDialog.setVisible(true);
        refresh();
    }//GEN-LAST:event_buttonTambahActionPerformed

    private void buttonUbahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonUbahActionPerformed
        if (tablePenghuni.getSelectedRow() == -1) {
            JOptionPane.showMessageDialog(this, "Pilih data yang akan diubah", "warning", JOptionPane.WARNING_MESSAGE);
        } else {
            Penghuni penghuni = listPenghuni.get(tablePenghuni.getSelectedRow());
            JDialog jDialog = new JDialog();
            jDialog.setModal(true);
            jDialog.setSize(435, 435);
            jDialog.add(new PenghuniUpdatePanel(jDialog, penghuni));
            jDialog.setLocationRelativeTo(null);
            jDialog.setVisible(true);
            refresh();
        }
    }//GEN-LAST:event_buttonUbahActionPerformed

    private void buttonHapusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonHapusActionPerformed
        if (tablePenghuni.getSelectedRow() == -1) {
            JOptionPane.showMessageDialog(this, "Pilih data yang akan dihapus", "warning", JOptionPane.WARNING_MESSAGE);
        } else {
            try {
                Penghuni penghuni = listPenghuni.get(tablePenghuni.getSelectedRow());
                PenghuniDAO penghuniDAO = new PenghuniDAOImpl();
                penghuniDAO.delete(penghuni);
                refresh();
            } catch (Exception ex) {
                Logger.getLogger(PenghuniPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_buttonHapusActionPerformed

    private void tablePenghuniMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablePenghuniMouseClicked
        if (kamar != null) {
            if (evt.getClickCount() == 2) {
                penghuniKamar = listPenghuni.get(tablePenghuni.getSelectedRow());
                jDialogKamar.dispose();
            }
        }
    }//GEN-LAST:event_tablePenghuniMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonHapus;
    private javax.swing.JButton buttonTambah;
    private javax.swing.JButton buttonUbah;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tablePenghuni;
    // End of variables declaration//GEN-END:variables

    private void refresh() {
        try {
            PenghuniDAO penghuniDAO = new PenghuniDAOImpl();
            listPenghuni = penghuniDAO.getAll(Penghuni.class);
            updateTablePenghuni();
        } catch (Exception ex) {
            Logger.getLogger(PenghuniPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void updateTablePenghuni() {
        String[] judul = {"No", "No Identitas", "Nama", "Jenis Kelamin", "Telp/HP", "Alamat"};
        Object[][] isi = new Object[listPenghuni.size()][6];
        int x = 0;
        int no = 0;
        for (Penghuni penghuni : listPenghuni) {
            no += 1;
            isi[x][0] = no;
            isi[x][1] = penghuni.getIdIdentitas();
            isi[x][2] = penghuni.getNama();
            isi[x][3] = penghuni.getKelamin().equals("1") ? "Pria" : "Wanita";
            isi[x][4] = penghuni.getTelp();
            isi[x][5] = penghuni.getAlamat();
            x++;
        }
        new ServiceHelper().setAutoRize(isi, judul, tablePenghuni);
    }
}
